require("../css/info.less");

let axios = require("../utils/axiosUtil.js");

let utils = require("../utils/utils.js");

const baseUrl = require("../utils/baseUrl.js");

window.onload = function () {
    let con = "";
    let cityId = "";
    let imgC = "";

    let genders = document.querySelector(".genders");
    let gendersText = document.querySelector(".gender-text");
    let year = document.querySelector(".year");
    let yearText = document.querySelector(".years");
    let pos = document.querySelector(".pos");
    let posText = document.querySelector(".pos-text");
    let ches = document.querySelector(".ches");
    let chesText = document.querySelector(".ches-text");
    let btn = document.querySelector(".btn");
    let inps = document.querySelector(".inp");
    let item = document.querySelector(".item1");
    let imgDo = document.querySelector(".imgD");
    let imgD = document.querySelector(".imgD-item img");
    let files = document.querySelector("#files");




    //性别
    genders.addEventListener("click", function () {
        weui.picker([{
            label: '男',
            value: 1
        }, {
            label: '女',
            value: 0
        }], {
                // onChange: function (res) {
                //     console.log(res[0].label);
                // },
                onConfirm: function (result) {
                    console.log(result[0].label);
                    gendersText.innerText = result[0].label;
                },
                title: '选择性别'
            });
    });

    //生日
    year.addEventListener("click", function () {
        console.log("点击了");

        weui.datePicker({
            start: 1990,
            end: new Date().getFullYear(),
            // onChange: function (result) {

            // },
            onConfirm: function (result) {
                let y = result[0].value;
                let m = result[1].value;
                let d = result[2].value;
                console.log(result);

                m = m < 10 ? "0" + m : m;
                d = d < 10 ? "0" + d : d;
                let date = `${y}-${m}-${d}`;
                yearText.innerText = date;

            },
            title: '选择日期'
        });




    })

    //省份
    pos.addEventListener("click", function () {
        axios.get("api/shared/province").then(function (res) {
            // console.log(res);
            let date = res.data.data.map(function (res1) {
                // console.log(res1);
                return {
                    label: res1.name,
                    value: res1.id
                }
            })
            // 生成picker
            weui.picker(date, {
                onConfirm: function (res2) {
                    posText.innerText = res2[0].label;
                    con = res2[0].value;
                    chesText.innerText = "未选择";
                },
                title: "选择省份"
            })
        })




    })

    //城市
    ches.addEventListener("click", function () {
        if (!con) {
            utils.toast(1, "请选择省份");
            return;
        }
        axios.get("/api/shared/city?provinceId=" + con).then(function (res) {
            let date = res.data.data.map(function (res1) {
                // console.log(res1);
                return {
                    label: res1.name,
                    value: res1.id
                }
            })
            // 生成picker
            weui.picker(date, {
                onConfirm: function (res2) {
                    chesText.innerText = res2[0].label;
                    cityId = res2[0].value;
                },
                title: "选择省份"
            })
        })






    })

    //点击保存/修改
    btn.addEventListener("click", function () {
        //非空验证
        if (!(inps.value && gendersText.innerText && yearText.innerText)) {
            utils.toast(1, "请输入信息");
        }
        // else if (!inps.value) {
        //     utils.toast(1, "请输入昵称");
        // }
        // else if (!gendersText.innerText) {
        //     utils.toast(1, "请选择性别");
        // }
        // else if (!yearText.innerText) {
        //     utils.toast(1, "请选择生日");
        // }
        //组装数据
        let date = {
            nickName: inps.value,
            gender: gendersText.innerText == "男" ? 1 : 0,
            provinceId: con,
            cityId: cityId,
            birthday: yearText.innerText,
            sign: item.value,
            imgUrl:imgC
        }
        //发送axios请求
        axios.post("/api/user/changeInfo", date).then(function (res) {
            console.log(res);


            if (res.data.errno == 0) {
                utils.toast(0, "保存成功")
            } else {
                utils.toast(1, "保存失败")
            }

        })





    })

    //默认数据加载 
    axios.get("/api/user/info").then(function (res) {
        let data = res.data.data
        console.log(data);

        //获取后端id值 如果有值 直接赋值给页面input框
        console.log(data);
        if (data.nickName) {
            inps.value = data.nickName;
        };
        //获取后端gender值 如果值1 直接赋值给页面男否则女
        if (data.gender == 1) {
            gendersText.innerText = "男";
        } else {
            gendersText.innerText = "女";
        };
        // 获取后端生日值 如果有值 直接赋值给页面
        if (data.birthday) {
            yearText.innerText = data.birthday;
        }
        //获取后端个人介绍 如果有 直接赋值给页面
        if (data.sign) {
            item.value = data.sign;
        }
        //获取后端省份id值 发送axios获取所以省份值 查找出当前id的省份
        if (data.provinceId) {
            axios.get("api/shared/province").then(function (res1) {
                // console.log(res1);
                let pro = res1.data.data.find(function (id) {
                    return id.id == data.provinceId
                })

                con = pro.id;

                posText.innerText = pro.name;



            })
        }

        //城市处理==拿到省份id==请求这个省份里面的所有城市列表
        if (data.cityId) {
            axios.get("/api/shared/city?provinceId=" + data.provinceId).then(function (res2) {
                let city = res2.data.data.find(function (item) {
                    return item.id == data.cityId
                })

                cityId = city.id;
                chesText.innerText = city.name;

            })
        }

        //头像
        imgD.src = baseUrl + data.imgUrl;
        //默认头像数据
        imgC = data.imgUrl;

    })

    //上传头像
    files.addEventListener("change", function () {
        //获取单个
        let file = files.files[0];
        // 将单个文件对象放到formData axios发送给后端
        let form = new FormData();
        //给form写入数据
        form.append("file", file);
        //发送axios
        axios.post("/api/shared/uploadPortrait1",form).then(function(res){    
            if(res.data.errno==0){
                utils.toast(0,"上传成功,请保存");
                let url = res.data.data.url;
                imgD.src = baseUrl + url;
                //值给图片的全局变量
                imgC = url;
            }else{
                utils.toast(1,"上传失败");
            }
        })
    })

    //d点击返还上一页
    let dom = document.querySelector(".item");
    dom.addEventListener("click", function () {
        location.href = "../mine.html"
    })

    //点击头像框实现上传
    imgDo.addEventListener("click",function(){
        files.click();
    })











}